#!/bin/bash

mem_total=$(free -m | sed -n '2p' | awk '{print $2}')

mem_used=$(free -m | sed -n '2p' | awk '{print $3}')

mem_free=$(free -m | sed -n '2p' | awk '{print $4}')

echo "$mem_total"
echo "$mem_used"
echo "$mem_free"

percent_mem_used=$(echo "scale=2; $mem_used / $mem_total * 100" | bc)

percent_mem_free=$(echo "scale=2; $mem_free / $mem_total * 100" | bc)

now_time=$(date +"%Y-%m-%d %H:%M%S 星期%w")

echo -e "\n"

echo -e "$now_time\n内存的使用率是: $percent_mem_used%"

echo -e "内存还剩: $percent_mem_free%未使用"

# 检查负载是否有压力

if [ $mem_used -gt 1 ]; then
  echo -e "\033[31;5m告警 \033[0m"
  echo -e "\033[31;5m内存使用率已经超过负载能力，目前使用率达到:$percent_mem_used% \033[0m"
else
  echo '目前内存负载正常'
fi

echo -e "\n"
